﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Openfree.Framework.DataAccess;
using System.Text;

public partial class PMSWeb_PersonWorkload : PMSBase
{
    private SqlCodeConfig codeConfig;

    string cmdText = @"select '0' as Row_No,T.UserName as UserName,
                    count(E.TaskItemID) as TaskCount,
                    cast(cast(sum(DATEDIFF(ss,E.StartTime,E.EndTime)) as float)/3600 as decimal(10,2)) as WorkTime,
                    sum(E.WorkTime) as WorkValue,
                    (SELECT C.CodeName  FROM " + SqlCodeConfig.TableName + @" C WHERE C.CodeValue=T.GroupCode AND C.Category='" + PMSCodeCategory.PMS_BMZB + @"') as GroupName 
                    from PMS_User T left join 
                    (select B.UserID,B.WorkTime,A.TaskItemID,A.StartTime,A.EndTime from PMS_TaskItem A ,PMS_TaskUser B where A.TaskItemID=B.TaskID {0}) E ON E.UserID=T.UserID 
                    WHERE 1=1 and T.IsStat='1' and T.GroupCode is not null and T.GroupCode <>'' ";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            string date = DateTime.Now.ToString("yyyy-MM-dd"); 
            string sql = string.Format(cmdText, " and DATEDIFF(MONTH,A.StartTime,'" + date + "')=0 ");
            FillGirdView(0, sql + " GROUP BY T.GroupCode,UserName ORDER BY T.GroupCode");
            InitLoadData(); 
        }
    }

    private void InitLoadData()
    {
        codeConfig = new SqlCodeConfig(this.Connection);
        DataSet ds = codeConfig.LoadDataByCategory(PMSCodeCategory.PMS_BMZB);
        this.BindDropDownList(S_GroupID, ds, true);
        int m = DateTime.Now.Month;
        S_Month.SelectByValue = "" + m;
    }
    private string GetSql()
    { 
        StringBuilder str = new StringBuilder();
        StringBuilder sql = new StringBuilder(cmdText);
        if (!string.IsNullOrEmpty(S_PersonName.Text))
        {
            sql.Append(" AND T.UserName like '%").Append(S_PersonName.Text).Append("%'");
        }
        if (!string.IsNullOrEmpty(S_Month.SelectedValue))
        {
            DateTime dt = DateTime.Now;
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string d = "";
            if (S_Month.SelectedValue.Length == 1)
            {
                d = date.Substring(0, 5) + "0" + S_Month.SelectedValue + "-01";
            }
            else
            {
                d = date.Substring(0, 5) + S_Month.SelectedValue + "-01";
            }
            str.Append(" and DATEDIFF(MONTH,A.StartTime,'" + d + "')=0 ");
        }
        if (!string.IsNullOrEmpty(S_GroupID.SelectedValue))
        {
            sql.Append(" AND T.GroupCode = '").Append(S_GroupID.SelectedValue).Append("'");
        } 
        return string.Format(sql.ToString(), str.ToString());
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sql = GetSql();
        FillGirdView(0, sql + " GROUP BY T.GroupCode,UserName ORDER BY T.GroupCode");
    }
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = ""+(e.Row.RowIndex+1);
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string sql = GetSql();
        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql + " GROUP BY T.GroupCode,UserName ORDER BY T.GroupCode");
        string filePath = Server.MapPath(SessionManager.TempVirtualFolder + System.Guid.NewGuid().ToString() + ".xls");
        string outinfo = DocumentHelper.ExportPersonWorkloadOne(PMSConst.PMSPersonWorkloadTemplatePath, filePath, ds, S_Month.SelectedValue);
        if (string.IsNullOrEmpty(outinfo))
        {
            base.DownLoadFile(filePath, "个人工作量统计.xls");
        }
        else
        {
            Alert(outinfo);
        } 
    }

    protected void btnExportAll_Click(object sender, EventArgs e)
    {
        string date = DateTime.Now.ToString("yyyy-MM-dd");
        string sql = string.Format(cmdText, " and DATEDIFF(YEAR,A.StartTime,'" +date + "')=0");

        DataSet ds = PMSAccess.GetDataSet(this.Connection, sql + " GROUP BY T.GroupCode,UserName ORDER BY T.GroupCode");
        string filePath = Server.MapPath(SessionManager.TempVirtualFolder + System.Guid.NewGuid().ToString() + ".xls");
        string outinfo = DocumentHelper.ExportPersonWorkloadAll(PMSConst.PMSPersonWorkloadTemplatePath, filePath, ds);
        if (string.IsNullOrEmpty(outinfo))
        {
            base.DownLoadFile(filePath, "个人工作量统计.xls");
        }
        else
        {
            Alert(outinfo);
        } 
    } 
}
